<?php
class Application_Form_Registration extends Zend_Form{
	
	/**
	 * @author Youssef Driouach
	 */
	
	public function init(){
		
		$this->setName('registration');
		$this->setMethod("post");
		
		//the first name element
		$this->addElement("text","firstName", array(
				'label'			=> 'First Name:',
				'required'  	=> true,
				'filters'    	=> array('StringTrim','StripTags'),
				));
		//the last name element
		$this->addElement("text", "LastName", array(
				'label'			=> 'Last Name:',
				'required'		=> true,
				'filters'		=> array('StringTrim', 'StripTags'),
				));
		
		//check if the e-mail exist in the Database
		$emailDoesntExist = new Zend_Validate_Db_NoRecordExists('user', 'email');
		$this->addElement("text", 'email', array(
	            'label'      => 'Your Email:',
	            'required'   => true,
	            'filters'    => array('StringTrim','StripTags'),
	            'validators' => array(
	                'EmailAddress',
	            	$emailDoesntExist,
	            	'NotEmpty'
	            )
        ));
		
		$this->addElement("text", 'reemail', array(
				'label'      => 'Re-enter Email:',
				'required'   => true,
				'filters'    => array('StringTrim','StripTags'),
				'validators' => array(
						'EmailAddress',
						$emailDoesntExist,
						'NotEmpty'
				)
		));
		
		$this->addElement("password", "password", array(
				'label'		 => 'Password:',
				'required'	 => true,
				'validators' => array(
						'NotEmpty'
				)
		));
		
		$sexCond = array('min' => 1, 'max' => 2);
		
		$sex = new  Zend_Form_Element_Select("sex");
		$locale = new Zend_Locale();
		$sex 	->setLabel('I am:')
				->setRequired(true)
				->addMultiOptions( array(
	            				'0' => 'Select sex',
	                            '1' => 'Male',
	                            '2' => 'Female',
	                            ))
				->addValidator('NotEmpty')
				->addValidator(new Zend_Validate_Between($sexCond))
				->setAttribs(array(
	                                'id'=>'sex_id',
	                            ))
				->addErrorMessage('You need to select one value');
		$this->addElements(array($sex));
		/*/
		$this->addElement('select', 'sex', array(
	            'label'      => 'I am:',
	            'required'   => true,
	            'attribs'	 =>   array(
	                                'id'=>'sex_id',
	                            ),
	            'multioptions'   => array(
	            				'0' => 'Select sex',
	                            '1' => 'Male',
	                            '2' => 'Female',
	                            ),
				'validators'	 => array(new Zend_Validate_Between($options)),
				'ErrorMessage'   => 'You need to select one value'
        ));
		*/
		$locale = new Zend_Locale();
		$this->addElement('select', 'country', array(
				'label'      	 => 'Country:',
				'required'   	 => true,
				'multioptions'   => Zend_Locale::getTranslationList('territory', $locale , 2)
				));
		
		$dayCond = array('min' => 1, 'max' => 31);
		$monthCond = array('min' => 1, 'max' => 12);
		$yearCond = array('min' => 1900, 'max' => date('Y'));
		//Birth Field
        $day = new Zend_Form_Element_Select('day');
        $day->setLabel('Birthday:');
        $day->class = 'date_d';
        $cday = array_combine(range(1, 31), range(1, 31)); 
        $day->addMultiOptions(array('-1' => 'Day:')+$cday);
        $day->addValidator(new Zend_Validate_Between($dayCond))
        	->addErrorMessage('You need to select one value');
        
        $months = array('1' => 'January', '2' => 'February','3' => 'March','4' => 'April','5' => 'May','6' => 'June','7' => 'July',
        				'8' => 'August','9' => 'September','10' => 'October','11' => 'November','12' => 'December');
        //Month Field
        $month = new Zend_Form_Element_Select('month');
        $month->setLabel(' ');
        $month->addMultiOptions(array('-1' => 'Month:')+$months);
        $month->class = 'date_m';
        $month->addValidator(new Zend_Validate_Between($monthCond))
        		->addErrorMessage('You need to select one value');
            
        //Year Field
        $year = new Zend_Form_Element_Select('year');
        $year->setLabel(' ');
        $cyear = array_combine(range(date('Y'),1900,-1), range(date('Y'),1900,-1));
        $year->addMultiOptions(array('-1' => 'Year:')+$cyear);
        $year->class = 'date_y';
        $year->addValidator(new Zend_Validate_Between($yearCond))
        ->addErrorMessage('You need to select one value');

        /*Ajout des Eléments*/
        $this->addElements(array($day, $month, $year));
		// Un bouton d'envoi
		$this->addElement('submit', 'submit', array(
				'ignore'   => true,
				'label'    => 'Register',
				'test'	   => 'registration'
		));
		
		$this->setDecorators( array( array('ViewScript', array('viewScript' => 'login/loginform.phtml'))));
	}
	
	public function isValid($data)
	{
		$password 	= $this->getElement('email');
		$password->addValidator(new Zend_Validate_EmailMatch($data['reemail']));
	
		return parent::isValid($data);
	}
}